// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import store from './store'
import './assets/style.less'
import { httpPost } from './lib/http'
Vue.use(iView)
Vue.config.productionTip = false
Vue.prototype.$httpPost = httpPost



//刷新时,防止登录的数据丢失
if (window.localStorage.user) {
  store.commit('setUser', JSON.parse(window.localStorage.user))
}
//检测需要登录的页面，如果未登录，先去登录
router.beforeEach((to, from, next) => {
  let requiresAuth = false,
      requiresRole = '',
      { role, uid } = store.getters.getUser
  to.matched.some(record => {
    requiresAuth = record.meta.requiresAuth
    requiresRole = record.meta.role
  })
  //验证是否需要登录
  if (requiresAuth) {
    //验证登录了吗
    if (uid) {
      //验证是否需要判断权限并且权限是否匹配
      if(requiresRole && role !== requiresRole) {
        next({ path: '/' })
      } else {
        next()
      }
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
